<script setup lang="ts">
import { reactive, onMounted } from 'vue'
import { Form } from 'ant-design-vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const useForm = Form.useForm

interface FormState {
  row: number
  col: number
  size: number
  num: number
  remember: boolean
}

onMounted(() => {})

const af = reactive<FormState>({
  row: 1,
  col: 1,
  size: 1,
  num: 1,
  remember: true
})

// 清空表单
const { resetFields } = useForm(af)

// 提交表单
const onFinish = (values: any) => {
  console.log('Success:', values)
  localStorage.setItem('val_list',JSON.stringify(values))
  setTimeout(() => router.push(`/game?id=3`), 1000)
}

// 表单提交失败
const onFinishFailed = (errorInfo: any) => {
  console.log('Failed:', errorInfo)
}
</script>
<template>
  <div class="main">
    <div class="setBox">
      <h1 class="center mg">设置游戏参数</h1>
      <a-form
        :model="af"
        name="basic"
        :label-col="{ span: 8 }"
        :wrapper-col="{ span: 16 }"
        autocomplete="off"
        @finish="onFinish"
        @finishFailed="onFinishFailed"
      >
        <a-form-item name="row" :rules="[{ required: true, message: '请输入个数' }]">
          <a-input v-model:value="af.row" placeholder="请输入行数" :min="1" />
        </a-form-item>
        <a-form-item name="col" :rules="[{ required: true, message: '请输入列数' }]">
          <a-input v-model:value="af.col" placeholder="请输入列数" :min="1" />
        </a-form-item>
        <a-form-item name="size" :rules="[{ required: true, message: '请输入网格尺寸' }]">
          <a-input v-model:value="af.size" placeholder="请输入网格尺寸" :min="1" />
        </a-form-item>
        <a-form-item name="num" :rules="[{ required: true, message: '请输入地雷数量' }]">
          <a-input v-model:value="af.num" placeholder="请输入地雷数量" :min="1" />
        </a-form-item>

        <div class="fot mg">
          <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
            <a-button type="primary" html-type="submit">提交</a-button>
          </a-form-item>
          <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
            <a-button html-type="reset" @click="resetFields">重置</a-button>
          </a-form-item>
          <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
            <a-button type="primary" @click="router.go(-1)">返回</a-button>
          </a-form-item>
        </div>
      </a-form>
    </div>
  </div>
</template>
<style scoped lang="scss">
.error-infos :deep(.ant-form-explain) {
  white-space: pre-line;
}
.setBox {
  background: #fff;
  padding: 30rpx;
  display: flex;
  justify-content: center;
  flex-flow: column;
  border-radius: 20px;
}
.ant-form {
  padding: 30px;
  width: 500px;
  height: 380px;
  transform: translate(80px);
}

.fot {
  display: flex;
  gap: 10px;
}
</style>
